package com.hoongs.v1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoongs.v1.dto.DictPageDto;
import com.hoongs.v1.entity.Dict;
import com.hoongs.v1.mapper.DictMapper;
import com.hoongs.v1.service.DictService;
import com.hoongs.v1.utils.PResult;
import com.hoongs.v1.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;

@Service
public class DictServiceImpl implements DictService {

    public static final String NOT_FOUND_ARTICLE = "没有找到该字典！";

    @Resource
    private DictMapper dictMapper;

    @Override
    public Result add(Dict dict) {
        if (dictMapper.insert(dict) == 1) {
            return Result.opSuccess();
        } else {
            return Result.opError();
        }
    }

    @Override
    public Result remove(String id) {
        if (!findArticleById(id)) return Result.opError(NOT_FOUND_ARTICLE);
        if (dictMapper.deleteById(id) == 1) {
            return Result.opSuccess();
        } else {
            return Result.opError();
        }
    }

    @Override
    public Result update(Dict dict) {
        if (!findArticleById(dict.getId())) return Result.opError(NOT_FOUND_ARTICLE);
        if (dictMapper.updateById(dict) == 1) {
            return Result.opSuccess();
        } else {
            return Result.opError();
        }
    }

    @Override
    public Result selectById(String id) {
        Dict dict = dictMapper.selectById(id);
        if (dict == null) return Result.error(NOT_FOUND_ARTICLE);
        return Result.success(dict);
    }

    @Override
    public Result selectList(String name, String parentId, String parentName) {
        QueryWrapper<Dict> wrapper = new QueryWrapper();
        wrapper.likeRight("value", "D");
        if (StringUtils.isNotBlank(name)) {
            wrapper.like("name", name);
        }
        if (StringUtils.isNotBlank(parentName)) {
            wrapper.like("parent_name", parentName);
        }
        if (StringUtils.isNotBlank(parentId)) {
            wrapper.eq("parent_id", parentId);
        }
        wrapper.orderBy(true, true, "sortcode");
        Collection<Dict> list = dictMapper.selectList(wrapper);
        return Result.success(list);
    }

    @Override
    public Result selectPageList(DictPageDto dictDto) {
        if (!PResult.checkPageQuery(dictDto.getPage(), dictDto.getSize())) return Result.error("分页参数错误");

        Page<Dict> daoPage = new Page<>(dictDto.getPage(), dictDto.getSize());
        QueryWrapper<Dict> wrapper = new QueryWrapper<Dict>();
        wrapper.likeRight("value","D");
        if (StringUtils.isNotBlank(dictDto.getName())) {
            wrapper.like("name", dictDto.getName());
        }
        if (StringUtils.isNotBlank(dictDto.getValue())) {
            wrapper.like("value", dictDto.getValue());
        }
        if (StringUtils.isNotBlank(dictDto.getParentName())) {
            wrapper.like("parent_name", dictDto.getParentName());
        }
        if (StringUtils.isNotBlank(dictDto.getParentId())) {
            wrapper.eq("parent_id", dictDto.getParentId());
        }
        wrapper.orderBy(true, true, "sortcode");
        IPage<Dict> dictDaoIPage = dictMapper.selectPage(daoPage, wrapper);
        PResult rs = new PResult(dictDaoIPage);
        return Result.success(rs);
    }

    /**
     * 根据Id查询文章是否存在
     */
    public Boolean findArticleById(String id) {
        Dict dict = dictMapper.selectById(id);
        return dict != null;
    }
}
